package edu.cuit.avatar.think;

import java.time.LocalDate;

/**
 * 贷款类
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/5 8:54
 */
public class Loan {
    /**
     * 贷款利率
     */
    private float annualInterestRate;
    /**
     * 贷款年限
     */
    private int numberOfYears;
    /**
     * 贷款金额
     */
    private float loanAmount;
    /**
     * 贷款日期
     */
    private LocalDate loanDate;

    public Loan() {
        loanDate = LocalDate.now();
    }

    public Loan(float annualInterestRate, int numberOfYears, float loanAmount) {
        this();
        this.annualInterestRate = annualInterestRate;
        this.numberOfYears = numberOfYears;
        this.loanAmount = loanAmount;
    }

    public float getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(float annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }

    public int getNumberOfYears() {
        return numberOfYears;
    }

    public void setNumberOfYears(int numberOfYears) {
        this.numberOfYears = numberOfYears;
    }

    public float getLoanAmount() {
        return loanAmount;
    }

    public void setLoanAmount(float loanAmount) {
        this.loanAmount = loanAmount;
    }

    public LocalDate getLoanDate() {
        return loanDate;
    }

    public void setLoanDate(LocalDate loanDate) {
        this.loanDate = loanDate;
    }

    /**
     * 计算每月偿还额
     * @return 每月偿还额
     */
    public float getMonthlyPayment(){
        float monthlyRate = this.annualInterestRate / 1200;
        //贷款本金×月利率×（1＋月利率）＾还款月数〕÷〔（1＋月利率）＾还款月数－1〕
        return (float)(loanAmount * monthlyRate /(1 - 1 / Math.pow(1 + monthlyRate, numberOfYears * 12)));
    }

    /**
     * 计算总偿还额
     * @return 总偿还额
     */
    public float getTotalPayment(){
        return numberOfYears * 12 * getMonthlyPayment();
    }

    /**
     * 打印该笔还款计划
     */
    public void printPaymentPlan(){
        for(int i = 1; i <= numberOfYears * 12; i++){
            System.out.printf("%s: %.2f\r\n", loanDate.plusMonths(i).toString(), getMonthlyPayment());
        }
    }
}
